package com.apes.fn.scm.merchant.account.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.fn.fico.debitnote.model.BusinessDebitNote;
import com.apes.fn.fico.debitnote.model.DeductionDetail;
import com.apes.fn.scm.merchant.settlement.model.MerchantSettlement;
import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.DateUtil;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Date;

/**
 * @BelongsProject: apes-server
 * @BelongsPackage: com.apes.fn.scm.merchant.account.model
 * @ClassName: SettlementBusinessDebitItem
 * @Author: pyb
 * @Description: 赋能商户结算借支明细
 * @CreateTime: 2021-09-29 16:21
 * @Version: 1.0
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicUpdate
@DynamicInsert
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = SettlementBusinessDebitItem.CONST_TABLE_NAME)
public class SettlementBusinessDebitItem extends BaseModel {
    public static final String CONST_TABLE_NAME = "merchant_business_item";
    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "赋能对账借支明细", padding = 8)
    @Comment("编码")
    private String id;


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "business_id", referencedColumnName = "id")
    @Comment("预支单")
    private BusinessDebitNote businessDebitNote;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "deductionDetail_id", referencedColumnName = "id")
    @Comment("预支明细")
    private DeductionDetail deductionDetail;


    @ManyToOne(optional = false)
    @JoinColumn(name = "merchant_settlement_id", referencedColumnName = "id", nullable = false)
    private MerchantAccountSettlement merchantSettlement;


    @Column(name = "current_amount", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("本期扣收金额")
    private double currentAmount = 0;

    @Column(name = "price_debit", nullable = false, columnDefinition = "NUMBER(16,4) DEFAULT 0")
    @Comment("借支金额")
    private double priceDebit = 0.0;

    @Column(name = "principal_repaid", nullable = false, columnDefinition = "NUMBER(16,4) DEFAULT 0")
    @Comment("往期已还本金")
    private double principalRepaid = 0.0;

    @Column(name = "balance_amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("结余金额")
    private double balanceAmount = 0;


    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Transient
    private Date approveDate;

    @Transient
    public Date getApproveDate() {
        if (businessDebitNote == null) return null;
        return businessDebitNote.getApproveDate();

    }


    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Transient
    private Date loanDate;

    public Date getLoanDate() {
        if (businessDebitNote == null) return null;
        return businessDebitNote.getLoanDate();

    }

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Transient
    private Date borrowingDate;

    public Date getBorrowingDate() {
        if (businessDebitNote == null) return null;
        return businessDebitNote.getBorrowingDate();

    }

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Transient
    private Date limitDate;

    public Date getLimitDate() {
        if (businessDebitNote == null) return null;
        return businessDebitNote.getLimitDate();

    }

    /**
     * 预支说明
     */
    @Transient
    private  String reason;

    @Transient
    public String getReason() {
        if (businessDebitNote == null) return null;
        return businessDebitNote.getReason();

    }
}
